//
//  BaseViewController.m
//  MenkNav
//
//  Created by Burn on 2019/6/14.
//  Copyright © 2019 menksoft. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@property(strong,nonatomic) MenkNavnigationBar *bar;

@property(strong,nonatomic) UIView *left;
@property(strong,nonatomic) UIView *mid;
@property(strong,nonatomic) UIView *right;
@property(strong,nonatomic) UIButton *backBTN;


@property (nonatomic, assign) CGRect playerFrame;   //记录播放器原始frame

@end
static CGFloat AnimationDuration = 0.3;     //旋转动画执行时间

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // !!!: Set Up Source
    [self source];
}

- (void)source{
    [self.view addSubview:self.bar];
//    [self.bar addSubview:self.backBTN];
//    self.backBTN.center = CGPointMake(20, self.bar.center.y + SafeAreaStatuBarHeight * .5);
}


- (MenkNavnigationBar *)bar{
    if (!_bar) {
        _bar = [[MenkNavnigationBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SafeAreaTopHeight)];
        _bar.dataSource = self;
    }
    return _bar;
}

- (UIButton *)backBTN{
    if (!_backBTN) {
        UIImage *image = [UIImage imageNamed:@"ic_nav_back_black"];
        _backBTN = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBTN setImage:image forState:UIControlStateNormal];
        _backBTN.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        
        [_backBTN addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];

    }
    return _backBTN;
}

- (void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}


- (UIView *)mk_navigationBarLeft:(MenkNavnigationBar *)navigationBar{
    return self.backBTN;
}


#pragma mark - 全屏相关Begin
- (void)setPlayerVC:(BOOL)playerVC{
    _playerVC = playerVC;
    if (playerVC) {
        //开启和监听 设备旋转的通知
        if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
            [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        }
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleDeviceOrientationChange:)
                                                    name:UIDeviceOrientationDidChangeNotification object:nil];
    }
}

- (void)setPlayerView:(UIView *)playerView{
    _playerView = playerView;
}

//设备方向改变的处理
- (void)handleDeviceOrientationChange:(NSNotification *)notification{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    switch (deviceOrientation) {
        case UIDeviceOrientationFaceUp:
            NSLog(@"屏幕朝上平躺");
            break;
        case UIDeviceOrientationFaceDown:
            NSLog(@"屏幕朝下平躺");
            break;
        case UIDeviceOrientationUnknown:
            NSLog(@"未知方向");
            break;
        case UIDeviceOrientationLandscapeLeft:
            if (self.isFullScreen) {
                [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
            }
            
            NSLog(@"屏幕向左横置");
            break;
        case UIDeviceOrientationLandscapeRight:
            if (self.isFullScreen) {
                [self interfaceOrientation:UIInterfaceOrientationLandscapeLeft];
            }
            
            NSLog(@"屏幕向右橫置");
            break;
        case UIDeviceOrientationPortrait:
            NSLog(@"屏幕直立");
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"屏幕直立，上下顛倒");
            break;
        default:
            NSLog(@"无法辨识");
            break;
    }
}


- (void)fullScreenAction{
    if (!self.isPlayerVC) { return; }
    if (self.isFullScreen) {//如果是全屏，点击按钮进入小屏状态
        [self changeToOriginalFrame];
    } else {//不是全屏，点击按钮进入全屏状态
        [self changeToFullScreen];
    }
    
}

- (void)changeToOriginalFrame {
    
    if (!self.isFullScreen) {
        return;
    }
    
    [UIView animateWithDuration:AnimationDuration animations:^{
        
        [self interfaceOrientation:UIInterfaceOrientationPortrait];
        self.playerView.frame = self.playerFrame;
        
    } completion:^(BOOL finished) {
        
        //        [self.playerView removeFromSuperview];
        
        //        [self.playerSuperView addSubview:self.playerView];
        self.isFullScreen = NO;
    }];
    
}


- (void)changeToFullScreen {
    if (self.isFullScreen) {
        return;
    }
    
    //记录播放器视图的父视图和原始frame值,在实际项目中，可能会嵌套子视图，所以播放器的superView有可能不是self.view，所以需要记录父视图
    //    self.playerSuperView = self.playerView.superview;
    self.playerFrame = self.playerView.frame;
    
    CGRect rectInWindow = [self.playerView convertRect:self.playerView.bounds toView:[UIApplication sharedApplication].keyWindow];
    self.playerView.frame = rectInWindow;
    
    //执行旋转动画
    [UIView animateWithDuration:AnimationDuration animations:^{
        
        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
        if (orientation == UIDeviceOrientationLandscapeRight) {     // 这里取反是 因为手机往左边转话屏应该是往右转
            [self interfaceOrientation:UIInterfaceOrientationLandscapeLeft];
        } else {
            [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
        }
        
        self.playerView.bounds = CGRectMake(0, 0, CGRectGetHeight([UIApplication sharedApplication].keyWindow.bounds), CGRectGetWidth([UIApplication sharedApplication].keyWindow.bounds));
        
        self.playerView.center = CGPointMake(CGRectGetMidX([UIApplication sharedApplication].keyWindow.bounds), CGRectGetMidY([UIApplication sharedApplication].keyWindow.bounds));
        
    } completion:^(BOOL finished) {
        
        self.isFullScreen = YES;
    }];
}



- (void)interfaceOrientation:(UIInterfaceOrientation)orientation {
    if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) {
        // 设置横屏
        [self toOrientation:orientation];
    } else if (orientation == UIInterfaceOrientationPortrait) {
        // 设置竖屏
        [self toOrientation:UIInterfaceOrientationPortrait];
    }
}



- (void)toOrientation:(UIInterfaceOrientation)orientation {
    // 获取到当前状态条的方向
    UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
    // 判断如果当前方向和要旋转的方向一致,那么不做任何操作
    if (currentOrientation == orientation) { return; }
    
    [[UIApplication sharedApplication] setStatusBarOrientation:orientation animated:NO];
    
    // 获取旋转状态条需要的时间:
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:AnimationDuration];
    // 更改了状态条的方向,但是设备方向UIInterfaceOrientation还是正方向的,这就要设置给你播放视频的视图的方向设置旋转
    // 给你的播放视频的view视图设置旋转
    self.playerView.transform = CGAffineTransformIdentity;
    self.playerView.transform = [self getTransformRotationAngle];
    // 开始旋转
    [UIView commitAnimations];
}

- (CGAffineTransform)getTransformRotationAngle {
    // 状态条的方向已经设置过,所以这个就是你想要旋转的方向
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    // 根据要进行旋转的方向来计算旋转的角度
    if (orientation == UIInterfaceOrientationPortrait) {
        return CGAffineTransformIdentity;
    } else if (orientation == UIInterfaceOrientationLandscapeLeft){
        return CGAffineTransformMakeRotation(-M_PI_2);
    } else if(orientation == UIInterfaceOrientationLandscapeRight){
        return CGAffineTransformMakeRotation(M_PI_2);
    }
    return CGAffineTransformIdentity;
}




//最后在dealloc中移除通知 和结束设备旋转的通知
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
}
//  自动旋转关闭
- (BOOL)shouldAutorotate {
    return NO;
}
//  支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight;
}
#pragma mark - 全屏相关End

@end
